﻿using ES.Client.Expression.Field;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace ES.Client.Utility
{
    public class FieldUtil
    {
        public static string GetFieldName<T>(Expression<Func<T, object>> expression)
        {
            try
            {
                if (expression == null || expression.Body == null)
                {
                    return "";
                }

                var parser = ParserFactory.Create(expression.Body.NodeType);
                var field = parser.Parse<T>(expression);
                return field;
            }
            catch
            {
                return "";
            }
        }

        public static string GetRootField(string field)
        {
            var _fields = field.Split(new char[1] { '.' }, StringSplitOptions.RemoveEmptyEntries).ToList();
            var rootField = _fields.FirstOrDefault();
            return rootField;
        }
    }
}
